package com.example.handheld4_gsignal_app.services;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 设置服务类 - 处理应用设置和配置
 */
public class SettingsService {
    
    private Context context;

    public SettingsService(Context context) {
        this.context = context;
    }

    /**
     * 获取应用版本
     */
    public String getAppVersion() {
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            return "1.0.0";
        }
    }

    /**
     * 获取设备信息
     */
    public String getDeviceInfo() {
        return Build.MANUFACTURER + " " + Build.MODEL + " (Android " + Build.VERSION.RELEASE + ")";
    }

    /**
     * 获取关于信息
     */
    public String getAboutInfo() {
        return "4G信号检测系统\n" +
               "版本: " + getAppVersion() + "\n" +
               "设备: " + getDeviceInfo() + "\n" +
               "开发团队: GSignal Team\n" +
               "版权所有 © 2024";
    }

    /**
     * 清除所有数据
     */
    public void clearAllData() {
        // 清除SharedPreferences
        context.getSharedPreferences("signal_data", Context.MODE_PRIVATE).edit().clear().apply();
        context.getSharedPreferences("user_settings", Context.MODE_PRIVATE).edit().clear().apply();
        
        // 清除缓存文件
        clearCacheFiles();
    }

    /**
     * 清除缓存文件
     */
    private void clearCacheFiles() {
        try {
            File cacheDir = context.getCacheDir();
            if (cacheDir != null && cacheDir.exists()) {
                deleteRecursive(cacheDir);
            }
        } catch (Exception e) {
            // 忽略清除错误
        }
    }

    /**
     * 递归删除文件
     */
    private void deleteRecursive(File fileOrDirectory) {
        if (fileOrDirectory.isDirectory()) {
            for (File child : fileOrDirectory.listFiles()) {
                deleteRecursive(child);
            }
        }
        fileOrDirectory.delete();
    }

    /**
     * 导出数据
     */
    public String exportData() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
            String fileName = "signal_data_export_" + sdf.format(new Date()) + ".txt";
            
            File exportFile = new File(context.getExternalFilesDir(null), fileName);
            FileWriter writer = new FileWriter(exportFile);
            
            // 写入导出数据
            writer.write("4G信号检测系统 - 数据导出\n");
            writer.write("导出时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date()) + "\n");
            writer.write("设备信息: " + getDeviceInfo() + "\n");
            writer.write("应用版本: " + getAppVersion() + "\n");
            writer.write("=====================================\n\n");
            
            // 这里可以添加实际的数据导出逻辑
            writer.write("数据导出功能待完善...\n");
            
            writer.close();
            return exportFile.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取存储使用情况
     */
    public String getStorageInfo() {
        try {
            File cacheDir = context.getCacheDir();
            long cacheSize = getFolderSize(cacheDir);
            
            File filesDir = context.getFilesDir();
            long filesSize = getFolderSize(filesDir);
            
            long totalSize = cacheSize + filesSize;
            
            return String.format("缓存: %.2f MB\n文件: %.2f MB\n总计: %.2f MB", 
                cacheSize / 1024.0 / 1024.0,
                filesSize / 1024.0 / 1024.0,
                totalSize / 1024.0 / 1024.0);
        } catch (Exception e) {
            return "无法获取存储信息";
        }
    }

    /**
     * 获取文件夹大小
     */
    private long getFolderSize(File folder) {
        long size = 0;
        if (folder != null && folder.exists()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        size += getFolderSize(file);
                    } else {
                        size += file.length();
                    }
                }
            }
        }
        return size;
    }
}